/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-5-14
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.rms.auditconfig.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.pearlsoft.commons.utils.UUIDUtil;
import com.pearlsoft.util.DateUtils;
import com.pearlsoft.util.StringUtils;
import com.pearlsoft.webapp.core.WebException;
import com.pearlsoft.webapp.core.dao.RecrodRowMapper;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.service.BaseSimpleJdbcService;
import com.pearlsoft.webapp.rms.StepStatus;

/**
 *描述： 
 *资源配置审批服务
 * 
 */
@Service
public class AuditConfigService extends BaseSimpleJdbcService{
	
	private final RecrodRowMapper gridRowMapper=AuditConfigRowMapperHelper.gridRowMapper();
	
	public Record getById(String stepId){
		String sql = "SELECT stepId,opinion,result,checkReport,flowId,applyId  FROM rms_flowstep  WHERE stepId=:stepId";
		Map<String,String> param = new HashMap<String,String>();
		param.put("stepId", stepId);
		Record record = this.getBaseSimpleJdbcDao().queryOne(sql,param,
			new  RecrodRowMapper<Record>(){
				@Override
				public Record mapRow(ResultSet rs, int rowNum)
						throws SQLException {				
					SimpleRecord recrod = new SimpleRecord();
					recrod.setValue("stepId", rs.getString(1));
					recrod.setValue("opinion", rs.getString(2));
					recrod.setValue("result", rs.getString(3));
					recrod.setValue("checkReport", rs.getString(4));	
					recrod.setValue("flowId", rs.getString(5));
					recrod.setValue("applyId", rs.getString(6));
					return recrod;
				}
			});
		return record;
	}
	
	
	/***
	 * 获取待审批的步骤list
	 * @param userId
	 * @return
	 */
	public List<Record> getAuditStepList(String userId,String stepType){
		String sql="";
		sql = sql +" select "; 
		sql = sql +" step.stepId,step.flowId,step.applyId,step.stepName,step.startTime,step.endTime,step.stepstatus,res.resName,u.user_name";
		sql = sql +" from rms_flowstep step left join rms_resapply res on step.applyId= res.applyId ";
		sql = sql +"left join sys_user u on res.authorId = u.user_Id ";
		sql = sql +"where step.authorId=:authorId and step.stepType =:stepType order by step.startTime desc	";
		Map<String,String> param = new HashMap<String,String>();
		param.put("authorId", userId);
		param.put("stepType", stepType);
		List<Record> list = this.getBaseSimpleJdbcDao().query(sql, param, gridRowMapper);
		return list;
	}
	
	/***
	 * 保存处理
	 * @param record
	 * @throws WebException
	 */
	public void save(Record record) throws WebException{
		String stepId =(String)record.getValue("stepId");
		String checkReport =(String)record.getValue("checkReport");
		String result =(String)record.getValue("result");
		String opinion =(String)record.getValue("opinion");
		ArrayList<Map> hostList=(ArrayList<Map>)record.getValue("hostList");
		String sql=" update rms_flowstep set result=:result,opinion=:opinion,checkReport=:checkReport where stepId=:stepId";
		Map<String,String> param = new HashMap<String,String>();
		param.put("result", result);
		param.put("opinion", opinion);
		param.put("checkReport", checkReport);
		param.put("stepId", stepId);
		this.getBaseSimpleJdbcDao().update(sql, param);
		String sqlhost =" update rms_hostconfig set hostName=:hostName, vmName=:vmName,hostIp=:hostIp where configId=:configId";
		for( Map map:hostList){
			String configId=(String)map.get("configId");
			String hostName=(String)map.get("hostName");
			String hostIp=(String)map.get("hostIp");
			Map<String,String> param2 = new HashMap<String,String>();
			param2.put("configId", configId);
			param2.put("hostName", hostName);
			param2.put("vmName", hostName);
			param2.put("hostIp", hostIp);
			this.getBaseSimpleJdbcDao().update(sqlhost, param2);
		}
		
	}
	
	/***
	 * 资源配置审批提交
	 * @param record
	 * @throws WebException
	 */
	public void apply(Record record)throws WebException{
		//保存或者更新资源配置审批
		this.save(record);
		String stepId =(String)record.getValue("stepId");
		//更新步骤状态
		String sql =" update rms_flowstep  set stepstatus=:stepstatus, endTime=:endTime where stepId=:stepId  ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("stepId", stepId);
		param.put("endTime", DateUtils.getCurrentDate());
		param.put("stepstatus", StepStatus.DEALED);
		this.getBaseSimpleJdbcDao().update(sql, param);
		//自动生成下一步骤
		this.saveNewStep3(stepId);
	}
	
	/***
	 * 资源审核步骤
	 * @param record
	 * @throws WebException
	 */
	private void saveNewStep3(String preStepId) throws WebException{
		Record preStep = this.getById(preStepId);
		String preResult =(String)preStep.getValue("result");
		if("0".equalsIgnoreCase(preResult)){
			this.endFlow(preStepId);
		}else{
			String stepId = UUIDUtil.get();
			String applyId = (String) preStep.getValue("applyId");
			String flowId = (String) preStep.getValue("flowId");
			
			String authorId = this.getAuthorId("2");
			String sql="insert into rms_flowstep(stepId,flowId,applyId,authorId,stepName,stepType,startTime,stepstatus,hasdeal)";
			sql= sql+"values( :stepId,:flowId,:applyId,:authorId,:stepName,:stepType,:startTime,:stepstatus,:hasdeal)";
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("stepId", stepId);
			param.put("flowId", flowId);
			param.put("applyId", applyId);
			param.put("authorId", authorId);
			param.put("stepName", "资源审核");
			param.put("stepType", "2");
			param.put("startTime", DateUtils.getCurrentDate());
			param.put("stepstatus",StepStatus.UNDEAL);		
			param.put("hasdeal", "0");
			this.getBaseSimpleJdbcDao().update(sql, param);
		}
		
		

	}
	/***
	 * 结束流程
	 * @param record
	 * @throws WebException
	 */
	private void endFlow(String preStepId) throws WebException{
		Record preStep = this.getById(preStepId);
		String applyId = (String) preStep.getValue("applyId");
		Map<String, Object> param = new HashMap<String, Object>();
		//把申请步骤设置为完成
		String sql="update rms_flowstep t set t.stepstatus=:stepstatus where t.applyId=:applyId and t.stepType='0'";
		param.put("applyId", applyId);
		param.put("stepstatus", StepStatus.ENDFAIL);
		//插入个人资源表
		this.getBaseSimpleJdbcDao().update(sql, param);
	}
	
	
	/**
	 * 根据步骤类型获取可以处理该步骤类型的人员
	 * @param stepType
	 * @return
	 */
	private String getAuthorId(String stepType){
		String roleName = "";
		if(stepType.equalsIgnoreCase("0")){
			roleName = "申请岗";
		} else if(stepType.equalsIgnoreCase("1")){
			roleName = "资源管理岗";
		}else if(stepType.equalsIgnoreCase("2")){
			roleName = "中心领导";
		}
		else if(stepType.equalsIgnoreCase("3")){
			roleName = "资源管理岗";
		}
		String sql="select t.USER_ID from sys_user_role  t left join sys_role r on t.ROLE_ID = r.ROLE_ID ";
		sql = sql +" where r.ROLE_NAME =:ROLE_NAME";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ROLE_NAME", roleName);
		List<Record> list = this.getBaseSimpleJdbcDao().query(sql, param, new  RecrodRowMapper<Record>(){
			@Override
			public Record mapRow(ResultSet rs, int rowNum)
					throws SQLException {	
				SimpleRecord recrod = new SimpleRecord();
				recrod.setValue("USER_ID", rs.getString(1));
				return recrod;
			}
		});
		Record record = list.get(0);
		return (String) record.getValue("USER_ID");
	}
	
}
